package com.lxm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxm.common.contants.BlogConstants;
import com.lxm.dto.Result;
import com.lxm.entity.BlogType;
import com.lxm.mapper.BlogTypeDao;
import com.lxm.service.BlogTypeService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class BlogTypeServiceImpl extends ServiceImpl<BlogTypeDao, BlogType> implements BlogTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result addBlogType(String typeName) {
        BlogType blogType = new BlogType();
        blogType.setName(typeName);
        boolean isSave = save(blogType);
        if (!isSave) return Result.fail(BlogConstants.BLOG_TYPE_ADD_FAIL);
        stringRedisTemplate.opsForHash().put(BlogConstants.BLOG_TYPE_KEY,
                blogType.getId().toString(), typeName);
        return Result.ok();
    }

    @Override
    public Result deleteBlogType(Long id) {
        boolean isRemove = removeById(id);
        if (!isRemove) return Result.fail(BlogConstants.BLOG_TYPE_DELETE_FAIL);
        stringRedisTemplate.opsForHash().delete(BlogConstants.BLOG_TYPE_KEY,
                id.toString());
        return null;
    }

    @Override
    public Result updateBlogType(Long id, String typeName) {
        BlogType blogType = new BlogType();
        blogType.setId(id);
        blogType.setName(typeName);
        boolean isUpdate = updateById(blogType);
        if (!isUpdate) return Result.fail(BlogConstants.BLOG_TYPE_UPDATE_FAIL);
        stringRedisTemplate.opsForHash().put(BlogConstants.BLOG_TYPE_KEY,
                blogType.getId().toString(), typeName);
        return Result.ok();
    }
}